﻿using System;
using System.Reflection;

namespace 案例2
{

    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            Type type;

            Console.WriteLine("创建Type引用");
            Console.WriteLine("\t方法1：使用typeof运算符");
            type = typeof(Student);
            Console.WriteLine("\t\t"+type+"\n");

            Console.WriteLine("\t方法2：使用对象GetType方法");
            type = student.GetType();
            Console.WriteLine("\t\t"+type + "\n");

            Console.WriteLine("\t方法3：调用Type类的静态方法GetType()");
            type = Type.GetType("案例2.Student");
            Console.WriteLine("\t\t" + type + "\n");

            Console.WriteLine("反射创建对象");
            Console.WriteLine("\t方法1：Activator.CreateInstance(type)创建");
            student = Activator.CreateInstance(type) as Student;
            student = Activator.CreateInstance(type,new Object[]{"192101040","强子初","男",21}) as Student;
            
            Console.WriteLine("\t方法2：获取构造函数创建对象");

            ConstructorInfo ctor = type.GetConstructor(new Type[] {});
            student = ctor.Invoke(null) as Student;

            ctor = type.GetConstructor(new Type[] {typeof(String), typeof(String), typeof(String), typeof(int) });
            student = ctor.Invoke(new Object[] { "192101040", "强子初", "男", 21 }) as Student;

            Console.WriteLine("动态调用方法");
            MethodInfo method = type.GetMethod("print");
            Console.WriteLine("\t"+method.Invoke(student,null));
        }
    }
}
